function setGesture(el){
    var obj={}; //定义一个对象
    var istouch=false;
    var start=[];
    var touch;
    document.addEventListener("touchstart",function(e){
        if(e.touches.length>=2){  //判断是否有两个点在屏幕上
            start = e.touches;  //得到第一组两个点
            obj.gesturestart&&obj.gesturestart.call(el); //执行gesturestart方法
        } else if (e.touches.length === 1) { // 判断是否一个点在屏幕上
            istouch = true;
            touch = e.touches[0]; // 得到第一组的点
            obj.gesturestart&&obj.gesturestart.call(el);
        }
    },false);
    document.addEventListener("touchmove",function(e){
        if (e.cancelable) {
            if (!e.defaultPrevented) {
                e.preventDefault();
            }
        }
        if(e.touches.length >= 2 && istouch) {
            var now = e.touches;  //得到第二组两个点
            var scale = (getDistance(now[0], now[1]) - getDistance(start[0],start[1])) / 1000; //得到缩放比例，getDistance是勾股定理的一个方法
            // e.scale = scale.toFixed(2);
            obj.gesturemove&&obj.gesturemove.call(el,scale.toFixed(2));  //执行gesturemove方法
        } else if(e.touches.length === 1 && istouch) {
            var now = e.touches[0];
            var rotation = getAngle(now, touch);
            // e.rotation = rotation;
            obj.touch&&obj.touch.call(el,rotation);
        }
    },false);
    document.addEventListener("touchend",function(e){
        if(istouch){
            istouch=false;
            obj.gestureend&&obj.gestureend.call(el);  //执行gestureend方法
        }
    },false);
    return obj;
}
function getDistance(p1, p2) {
    var x = p2.pageX - p1.pageX,
        y = p2.pageY - p1.pageY;
    return Math.sqrt((x * x) + (y * y));
}
function getAngle(p1, p2) {
    var x = (p1.pageX - p2.pageX) / 100;
    var y = (p1.pageY - p2.pageY) / 100;
    return {x: x.toFixed(2), y: y.toFixed(2)};
}

var box=document.querySelector("#box");
console.log(box,'box')
var boxGesture=setGesture(box);  //得到一个对象
boxGesture.gesturestart=function(){  //双指开始
    // box.style.backgroundColor="yellow";
};
boxGesture.gesturemove=function(e){  //双指移动
    var scale = box.getAttribute("scale");
    var scaleX = (parseFloat(scale.x) + parseFloat(e)).toFixed(2);

    if (scaleX >= 0.1 && scaleX <= 0.25) {
        scale.x = scale.y = scale.z = scaleX;
        box.setAttribute("scale", scale);
    }

    // box.innerHTML = e.scale+"<br />"+e.rotation;
    // box.style.transform="scale("+e.scale+") rotate("+e.rotation+"deg)";//改变目标元素的大小和角度
};
boxGesture.touch = function (e) { //单指移动

    var rotation = box.getAttribute("rotation");
    // var rotationY = (parseFloat(rotation.y) + parseFloat(e.x));
    var rotationX = (parseFloat(rotation._y) + parseFloat(e.x)).toFixed(2);
    // if (rotationY > 360 || rotationY < -360) {
    //     rotationY = 0;
    // }
    if (rotationX > 360 || rotationX < -360) {
        rotationX = 0;
    }
    // rotation.y =  rotationY;
    rotation.y = rotationX;
    box.setAttribute("rotation", rotation);
};
boxGesture.gestureend=function(){  //双指结束
    // box.innerHTML="";
    // box.style.backgroundColor="red";
};